package de.jowadroid.timekeeping.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseController extends SQLiteOpenHelper {

	// TODO johannes find out if sqlite is case sensitve
	/** Table name */
	public static final String TABLE_WORKTIME = "worktimes";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_TIMESTAMP = "timestamp";
	public static final String COLUMN_START_STOP = "start_stop";
	public static final String COLUMN_WEEK_NUMBER = "weekNumber";

	//TODO Johannes: Remove weekNumber column
	
	private final static String DATABASE_NAME = "worktime.db";
	private static int DATABASE_VERSION = 1;

	// Database creation sql statement
	//sqlite has no time types if time is stored as int it is treated as seconds since 1970
	//http://www.sqlite.org/datatype3.html
	//sqlite datatypes are NULL, integer, real, text, blob
	private static final String DATABASE_CREATE = "create table "
	    + TABLE_WORKTIME + "(" 
	    + COLUMN_ID + " integer primary key autoincrement, " 
	    + COLUMN_TIMESTAMP + " integer not null, " 
	    + COLUMN_START_STOP + " text not null, "
	    + COLUMN_WEEK_NUMBER + " integer not null)";
	

	public DatabaseController(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
	}

	// This method is called during an upgrade of the database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//log into logCat
		Log.w(DatabaseController.class.getName(),
	        "Upgrading database from version " + DATABASE_VERSION + " to "
	            + DATABASE_VERSION+1 + ", which will destroy all old data");
	    db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKTIME);
	    onCreate(db);

	}
}
